# -*- coding: utf-8 -*-
import scrapy
from MyScrapy.items import MeituanGoods


class TianmaoSpider(scrapy.Spider):
    name = 'tianmao'
    proxy_list = []
    allowed_domains = ['tmall.com']
    start_urls = ['https://list.tmall.com/search_product.htm?q=%B3%E8%CE%EF']

    def parse(self, response):
        product_list = response.xpath('//div[@class="product-iWrap"]')
        if not len(product_list):
            print('product_list is null')
        for product in product_list:
            item = MeituanGoods()
            item['name'] = product.xpath('p[@class="productTitle"]/a/@title').get('').strip()
            item['price'] = product.xpath('p[@class="productPrice"]/em/@title').get('').strip()
            sales = product.xpath('p[@class="productStatus"]/span/em/text()').get('').strip()
            length = len(sales)
            if '万' in sales:
                num = sales[:length - 2]
                num = float(num) * 10000
                num = round(num)
                num = str(num)
            else:
                num = sales[:length - 1]
            item['sales'] = num
            yield item

